﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMFramework.Network;
using System.IO;

namespace UM_SERVER.Packet
{
    /// <summary>
    ///     包帮助类
    /// </summary>
    class PackageHelper
    {
        private static Dictionary<Protocol.NetworkProtocol, IParser> Parsers = new Dictionary<Protocol.NetworkProtocol, IParser>();

        public PackageHelper()
        {
            Parsers.Add(Protocol.NetworkProtocol.Base,new BaseProtocolParser());
        }
        /// <summary>
        ///     从完整包数据中分析出包协议簇
        /// </summary>
        /// <param name="Content" type="string">
        ///     <para>
        ///         包内容
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回包协议簇
        /// </returns>
        public static Protocol.NetworkProtocol GetProtocol(Dictionary<String,byte[]> Content)
        {
            //Content = "$Package&#[{C}]#&0&#[{C}]#&0&#[{C}]#&1&#[{C}]#&1&#[{C}]#&HASHCODE";
            return (Protocol.NetworkProtocol)BitConverter.ToInt32(Content["PROTOCOL"], 0);
        }

        /// <summary>
        ///     安装包解析器
        /// </summary>
        /// <param name="protocol">解析协议</param>
        /// <param name="parser">解析器</param>
        /// <returns>返回false, 则表示安装协议失败</returns>
        public bool InstallParser(Protocol.NetworkProtocol protocol, IParser parser)
        {
            if (!Equals(protocol))
            {
                Parsers.Add(protocol, parser);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///     对比当前包解析器中是否已经存在了指定协议解析器
        /// </summary>
        /// <param name="protocol">解析器协议</param>
        /// <returns>返回false, 表示不存在</returns>
        public bool Equals(Protocol.NetworkProtocol protocol)
        {
            foreach (var parser in Parsers)
            {
                if (((Protocol.NetworkProtocol)parser.Key) == protocol)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        ///     得到包命令
        /// </summary>
        /// <param name="Content" type="string">
        ///     <para>
        ///         包内容
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回包命令
        /// </returns>
        public static Protocol.IN_NetworkCommand GetNetworkCommand(Dictionary<String, byte[]> Content)
        {
            //Content = "$Package&#[{C}]#&0&#[{C}]#&0&#[{C}]#&1&#[{C}]#&1&#[{C}]#&HASHCODE";
            return (Protocol.IN_NetworkCommand)BitConverter.ToInt32(Content["COMMAND"], 0);
        }

        /// <summary>
        ///     得到聊天协议的包命令
        /// </summary>
        /// <param name="Content" type="string">
        ///     <para>
        ///         包内容
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回聊天协议包命令
        /// </returns>
        public static Protocol.TALK_IN_NetworkCommand GetTalkNetworkCommand(Dictionary<String, byte[]> Content)
        {
            //Content = "$Package&#[{C}]#&0&#[{C}]#&0&#[{C}]#&1&#[{C}]#&1&#[{C}]#&HASHCODE";
            return (Protocol.TALK_IN_NetworkCommand)BitConverter.ToInt32(Content["COMMAND"], 0);
        }

        /// <summary>
        ///     从指定索引出处得包头数据
        /// </summary>
        /// <param name="Content" type="string">
        ///     <para>
        ///         包完整数据
        ///     </para>
        /// </param>
        /// <param name="Index" type="int">
        ///     <para>
        ///         索引
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回指定索引处的包头数据
        /// </returns>
        public static String GetPackageHeaderByIndex(String Content, int Index)
        {
            return 
                Content.Split(new String[] { Network_OrderFlag.ORDER_TOTAL_SPLIT_FLAG },
                              StringSplitOptions.RemoveEmptyEntries)[0].Split(
                    new String[] { Network_OrderFlag.ORDER_CENTER_SPLIT }, StringSplitOptions.RemoveEmptyEntries)[Index];
        }

        /// <summary>
        ///     得到包体数据
        /// </summary>
        /// <param name="Content" type="string">
        ///     <para>
        ///         完整包数据
        ///     </para>
        /// </param>
        /// <returns>
        ///     A返回包体数据
        /// </returns>
        public static byte[] GetPackageBody(String Content)
        {
            return
                Encoding.Default.GetBytes(Content.Split(new String[] {Network_OrderFlag.ORDER_TOTAL_SPLIT_FLAG},
                              StringSplitOptions.RemoveEmptyEntries)[1]);
        }

        public static IParser FindParser(Protocol.NetworkProtocol Protocol)
        {
            return Parsers[Protocol];
        }
    }
}
